package cn.rongdao.jrkabao.ui.cashadvance;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frwidget.recyclerview.PtrRecyclerViewUIComponent;
import com.mifengkong.frwidget.refersh.OnPullToRefreshListener;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.cashadvance.TransactionRecordBean;
import cn.rongdao.jrkabao.bean.deposit.SelectMonthBean;
import cn.rongdao.jrkabao.model.cashadvance.TransactionRecordModel;
import cn.rongdao.jrkabao.ui.cashadvance.adapter.TransactionRecordAdapter;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.CashAdvanceTimeUIComponent;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.CashEmptyUIComponent;
import cn.rongdao.jrkabao.ui.deposit.popup.SelectMonthPopupUtil;

/**
 * Created by jack on 2017/7/28
 */

public class CashAdvanceRecordActivity extends BaseAutoFinishActivity {

    public static void start(Context context) {
        FRStartActivity.start(context, CashAdvanceRecordActivity.class);
    }

    @BindView(R.id.acar_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.acar_time)
    CashAdvanceTimeUIComponent cashAdvanceTimeUIComponent;
    @BindView(R.id.acar_empty_view)
    CashEmptyUIComponent cashEmptyUIComponent;
    @BindView(R.id.acar_ptr_framelayout)
    PtrRecyclerViewUIComponent ptrRecyclerViewUIComponent;

    private TransactionRecordAdapter transactionRecordAdapter;
    private TransactionRecordBean transactionRecordBean;

    @Override
    protected void initView() {
        initTitleBar();
        initPtr();
        initListener();
    }

    private void initListener() {
        ptrRecyclerViewUIComponent.setOnPullToRefreshListener(new OnPullToRefreshListener() {
            @Override
            public void onPullToRefresh() {
                getData();
                ptrRecyclerViewUIComponent.setCanRefresh(false);
            }
        });

        cashAdvanceTimeUIComponent.setOnSelectMonthClickListener(new SelectMonthPopupUtil.OnSelectMonthClickListener() {
            @Override
            public void getPosition(int position) {
                if (position < transactionRecordBean.getMonth().size()) {
                    cashAdvanceTimeUIComponent.setTimeContent(transactionRecordBean.getMonth().get(position));
                }
                if ((position + 1) <= transactionRecordBean.getTrade().size()
                        && null != transactionRecordBean.getTrade().get(position)
                        && transactionRecordBean.getTrade().get(position).size() > 0) {
                    ptrRecyclerViewUIComponent.setVisibility(View.VISIBLE);
                    transactionRecordAdapter.setDataList(transactionRecordBean.getTrade().get(position));
                    return;
                }
                ptrRecyclerViewUIComponent.setVisibility(View.GONE);
                cashEmptyUIComponent.initTransactionecordEmpty();
            }
        });
    }

    private void getData() {
        TransactionRecordModel.getInstance().execute(new FRBaseObserver<TransactionRecordBean>(this) {
            @Override
            protected void onBaseNext(TransactionRecordBean data) {
                cashAdvanceTimeUIComponent.setImageRes();
                if (null != data) {
                    cashAdvanceTimeUIComponent.setTimeContent("");
                    transactionRecordBean = data;
                    ptrRecyclerViewUIComponent.setVisibility(View.VISIBLE);
                    if (null != data.getTrade() && null != data.getTrade().get(0) && data.getTrade().get(0).size() > 0) {
                        transactionRecordAdapter.setDataList(data.getTrade().get(0));
                        ptrRecyclerViewUIComponent.setVisibility(View.VISIBLE);
                    } else {
                        ptrRecyclerViewUIComponent.setVisibility(View.GONE);
                        cashEmptyUIComponent.initTransactionecordEmpty();
                    }
                    if (null != data.getMonth()) {
                        cashAdvanceTimeUIComponent.setMonthDataList(getSelectMonthList(data.getMonth()));
                    }
                } else {
                    ptrRecyclerViewUIComponent.setVisibility(View.GONE);
                    cashEmptyUIComponent.initTransactionecordEmpty();
                }
                ptrRecyclerViewUIComponent.refreshComplete();
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                ptrRecyclerViewUIComponent.setVisibility(View.GONE);
                cashEmptyUIComponent.initTransactionecordEmpty();
                ptrRecyclerViewUIComponent.refreshComplete();
            }
        });
    }

    private List<SelectMonthBean> getSelectMonthList(List<String> monthList) {
        List<SelectMonthBean> list = new ArrayList<>();
        SelectMonthBean data;
        for (int i = 0; i < monthList.size(); i++) {
            data = new SelectMonthBean();
            if (i == 0) {
                data.setSelected(true);
            }
            data.setTime(monthList.get(i));
            list.add(data);
        }
        return list;
    }

    private void initPtr() {
        ptrRecyclerViewUIComponent.setBackground(R.color.layout_bg);
        transactionRecordAdapter = new TransactionRecordAdapter(this);
        ptrRecyclerViewUIComponent.setLayoutManager(new LinearLayoutManager(this));
        ptrRecyclerViewUIComponent.setAdapter(transactionRecordAdapter);
        ptrRecyclerViewUIComponent.delayRefresh(100);
    }

    private void initTitleBar() {
        titleBarUIComponent.initTitle("取现记录");
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary));
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected int layoutRes() {
        return R.layout.activity_cash_advance_record;
    }
}
